package com.action.front;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import com.entity.User;
import com.entity.other.Academy;
import com.entity.other.City;
import com.entity.other.Domitory;
import com.entity.other.Identify;
import com.entity.other.Industry;
import com.entity.other.IndustryBranch;
import com.entity.other.Junior;
import com.entity.other.Position;
import com.entity.other.PositionBranch;
import com.entity.other.Province;
import com.entity.other.Senior;
import com.entity.other.TechnicalSchool;
import com.entity.other.University;
import com.entity.userInfo.BasicInfo;
import com.entity.userInfo.ContactInfo;
import com.entity.userInfo.JuniorInfo;
import com.entity.userInfo.PersonalInfo;
import com.entity.userInfo.PrimaryInfo;
import com.entity.userInfo.SeniorInfo;
import com.entity.userInfo.TechnicalSchoolInfo;
import com.entity.userInfo.UniversityInfo;
import com.entity.userInfo.WorkInfo;
import com.enums.Day;
import com.enums.Month;
import com.enums.Year;
import com.opensymphony.xwork2.ActionContext;
import com.service.profile.ProfileService;
import com.util.abstractAction.AbstractAction;

public class ProfileAction extends AbstractAction{
	
	private static final long serialVersionUID = 1266959220307460583L;

	@Autowired
	private ProfileService profileService;
	
	private User user;
	
	private List<Province> provinceList;
	
	private List<City> cityList;
	
	private int provinceId;
	
	private List<University> universityList;
	
	private List<BasicInfo> basicInfoList;
	
	private BasicInfo basicInfo;
	
	private List<SeniorInfo> seniorInfoList;
	
	private List<TechnicalSchoolInfo> technicalSchoolInfoList;
	
	private List<JuniorInfo> juniorInfoList;
	
	private List<PrimaryInfo> primaryInfoList;
	
	private List<PersonalInfo> personalInfoList;
	
	private PersonalInfo personalInfo;
	
	private List<UniversityInfo> universityInfoList;
	
	private List<Academy> academyList;
	
	private List<Senior> seniorList;
	
	private List<TechnicalSchool> technicalSchoolList;
	
	private List<Junior> juniorList;
	
	private List<WorkInfo> workInfoList;
	
	private List<Industry> industryList;
	
	private List<IndustryBranch> industryBranchList;
	
	private List<Position> positionList;
	
	private List<PositionBranch> positionBranchList;
	
	private List<ContactInfo> contactInfoList;
	
	private ContactInfo contactInfo;
	
	private List<City> homeCityList;
	
	private List<City> locationCityList;
	
	private String birthDay;
	
	private String birthYear;
	
	private String birthMonth;
	
	private List<Year> yearList;
	
	private List<Month> monthList;
	
	private List<Day> dayList;
	
	private List<Identify> identifyList;
	
	private List<Domitory> domitoryList;
	
	private Map<Integer, List<Academy>> academyMap;
	
	private Map<Integer, List<Domitory>> domitoryMap;
	
	private Map<Integer, List<PositionBranch>> positionBranchMap;
	
	private Map<Integer, List<IndustryBranch>> industryBranchMap;
	
	private University university;
	
	private UniversityInfo universityInfo;
	
	private int universityId;
	
	private int positionId;
	
	private int industryId;
	
	private int workInfoId;
	
	private WorkInfo workInfo;
	
	public String toProfile(){
		return "toProfile";
	}
	
	public String toTest(){
		return "toTest";
	}

	
	public String toInformation(){
		try{
			 provinceList=profileService.getAllProvince();
			 universityList=profileService.getAllUniversity();
			// academyList=profileService.getAllAcademy();
			 seniorList=profileService.getAllSenior();
			 technicalSchoolList=profileService.getAllTechnicalSchool();
			 juniorList=profileService.getAllJunior();
			 industryList=profileService.getAllIndustry();
			 industryBranchList=profileService.getAllIndustryBranch();
			 positionList=profileService.getAllPosition();
			 positionBranchList=profileService.getAllPositionBranch();
			 identifyList=profileService.getAllIdentify();
			 
			 //年月日列表
			 yearList=Year.getYearList();
			 monthList=Month.getMonthList();
			 dayList=Day.getdayList();
			 
			 //基本信息
			 user=(User)ActionContext.getContext().getSession().get("user");
			 basicInfoList=profileService.getBasicInfoByUserId(user.getId());
			 if(basicInfoList!=null&&basicInfoList.size()>0){
			     basicInfo=basicInfoList.get(0);
			     homeCityList=profileService.getCityByProvinceId(basicInfo.getHomeProvinceId());
			     locationCityList=profileService.getCityByProvinceId(basicInfo.getLocationProvinceId());
			     String[] b=basicInfo.getBirthDay().toString().split("-");
			     birthYear=b[0];
			     birthMonth=b[1];
			     birthDay=b[2];
			 }
			 
			 //学校信息
			 seniorInfoList=profileService.getSeniorByUserId(user.getId());
			 technicalSchoolInfoList=profileService.getTechnicalSchoolInfoByUserId(user.getId());
			 juniorInfoList=profileService.getJuniorByUserId(user.getId());
			 primaryInfoList=profileService.getPrimaryInfoByUserId(user.getId());
			 universityInfoList=profileService.getUniversityByUserId(user.getId());
			 
			 domitoryMap=new HashMap<Integer, List<Domitory>>();
			 academyMap=new HashMap<Integer, List<Academy>>();
		     for (UniversityInfo universityInfo : universityInfoList) {
		    	 domitoryMap.put(universityInfo.getId(),profileService.getDomitoryByUniversityId(universityInfo.getUniversityId()));
			     academyMap.put(universityInfo.getId(), profileService.getAcademyByUniversityId(universityInfo.getUniversityId()));
		     }
			 
			 //工作信息
			 workInfoList=profileService.getWorkInfoByUserId(user.getId());
			 industryBranchMap=new HashMap<Integer, List<IndustryBranch>>();
			 positionBranchMap=new HashMap<Integer, List<PositionBranch>>();
			 for(WorkInfo workInfo:workInfoList){
				 industryBranchMap.put(workInfo.getId(), profileService.getIndustryBranchByIndustryId(workInfo.getIndustryId()));
				 positionBranchMap.put(workInfo.getId(), profileService.getPositionBranchByPositionId(workInfo.getPositionId()));
			 }
			 
			 //个人信息
			 personalInfoList=profileService.getPersonalInfoByUserId(user.getId());
			 if(personalInfoList!=null&&personalInfoList.size()>0)
			    personalInfo=personalInfoList.get(0);
			 
			 //联系方式
			 contactInfoList=profileService.getContactInfoByUserId(user.getId());
			 if(contactInfoList!=null&&contactInfoList.size()>0)
				 contactInfo=contactInfoList.get(0);
		}catch (Exception e) {
			e.printStackTrace();
		}
		return "toInformation";
	}
	
	public String saveBasicInfo(){
		try {
			user=(User)ActionContext.getContext().getSession().get("user");
			basicInfo.setUserId(user.getId());
			basicInfo.setBirthDay(birthYear+"-"+birthMonth+"-"+birthDay);
			profileService.updateBasicInfo(basicInfo);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "redirectToInformation";
	}
	
	//省市联动
	 public void provinceCity(){
 	    try {
 	    	cityList=profileService.getCityByProvinceId(provinceId);
 	    	String jsongStr="";
            for(City city:cityList){
            	jsongStr+="<option value=\""+city.getId()+"\">"+city.getCityName()+"</option>";
            }
            print(jsongStr);
         }  catch (Exception e) {
            
         }
  }
	 //学校跟专业宿舍联动
	 public void schoolAcademyDomitory(){
		 try{
			 academyList=profileService.getAcademyByUniversityId(universityId);
			 domitoryList=profileService.getDomitoryByUniversityId(universityId);
			 String jsonString="";
			 String jsonString1="";
			 String jsonString2="";
			 for (Academy academy:academyList) {
				 jsonString1+="<option value='"+academy.getId()+"'>"+academy.getAcademyName()+"</option>";
			    }
			 for (Domitory domitory:domitoryList) {
				  jsonString2+="<option value='"+domitory.getId()+"'>"+domitory.getDomitoryName()+"</option>";	
				}
			 jsonString = "{\"academy\":" +"\""+ (jsonString1) +"\""+ ",\"domitory\":"+"\""+(jsonString2)+"\""+"}";
			 System.out.println(jsonString);
		    print(jsonString);
		 }catch (Exception e) {
			// TODO: handle exception
		}
	 }
	 
	 public void positonBranchByPositon(){
		 try{
			 positionBranchList=profileService.getPositionBranchByPositionId(positionId);
			 String jsonString="";
			 for(PositionBranch positionBranch:positionBranchList){
				 jsonString+="<option value=\""+positionBranch.getId()+"\">"+positionBranch.getPositionBranchName()+"</option>";
			 }
			 print(jsonString);
		 }catch (Exception e) {
			e.printStackTrace();
		}
	 }
	 
	 public void industryBranchByIndustry(){
		 try{
		  industryBranchList=profileService.getIndustryBranchByIndustryId(industryId);
		  String jsonString="";
		  for(IndustryBranch industryBranch:industryBranchList){
				 jsonString+="<option value=\""+industryBranch.getId()+"\">"+industryBranch.getIndustryBranchName()+"</option>";
			 }
		  print(jsonString);
		 }catch (Exception e) {
				e.printStackTrace();
		}
    }  
	
	public String saveUniversityInfo(){
		try {
			user=(User)ActionContext.getContext().getSession().get("user");
			universityInfo.setUserId(user.getId());
			profileService.saveUniversityInfo(universityInfo);
		} catch (Exception e) {
			// TODO: handle exception
		}
		return "redirectToInformation";
	}
	
	public void ajaxDelUniversity(){
		try{
			profileService.delUniversityById(universityId);
			print(true);
		}catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void ajaxDelWorkInfo(){
		try{
			profileService.delWorkInfoById(workInfoId);
			print(true);
		}catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public String saveWorkInfo(){
		try{
			user=(User)ActionContext.getContext().getSession().get("user");
			workInfo.setUserId(user.getId());
			if(workInfo.getNowIn().toString().equals("1")){
				workInfo.setEndMonth(-1);
				workInfo.setEndYear(-1);
			}
			profileService.saveWorkInfo(workInfo);
		}catch (Exception e) {
			e.printStackTrace();
		}
		return "redirectToInformation";
	}
	
	public String savePersonInfo(){
		try {
			user=(User)ActionContext.getContext().getSession().get("user");
			personalInfo.setUserId(user.getId());
			profileService.savePersonInfo(personalInfo);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "redirectToInformation";
	}
	
	public String saveContactInfo(){
		try {
			user=(User)ActionContext.getContext().getSession().get("user");
			contactInfo.setUserId(user.getId());
			profileService.saveContactInfo(contactInfo);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "redirectToInformation";
	}
	

	public List<Province> getProvinceList() {
		return provinceList;
	}

	public void setProvinceList(List<Province> provinceList) {
		this.provinceList = provinceList;
	}

	public List<City> getCityList() {
		return cityList;
	}

	public void setCityList(List<City> cityList) {
		this.cityList = cityList;
	}

	public int getProvinceId() {
		return provinceId;
	}

	public void setProvinceId(int provinceId) {
		this.provinceId = provinceId;
	}

	public List<University> getUniversityList() {
		return universityList;
	}

	public void setUniversityList(List<University> universityList) {
		this.universityList = universityList;
	}

	public BasicInfo getBasicInfo() {
		return basicInfo;
	}

	public void setBasicInfo(BasicInfo basicInfo) {
		this.basicInfo = basicInfo;
	}

	public List<TechnicalSchoolInfo> getTechnicalSchoolInfoList() {
		return technicalSchoolInfoList;
	}

	public void setTechnicalSchoolInfoList(
			List<TechnicalSchoolInfo> technicalSchoolInfoList) {
		this.technicalSchoolInfoList = technicalSchoolInfoList;
	}

	public List<PrimaryInfo> getPrimaryInfoList() {
		return primaryInfoList;
	}

	public void setPrimaryInfoList(List<PrimaryInfo> primaryInfoList) {
		this.primaryInfoList = primaryInfoList;
	}

	public PersonalInfo getPersonalInfo() {
		return personalInfo;
	}

	public void setPersonalInfo(PersonalInfo personalInfo) {
		this.personalInfo = personalInfo;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public List<SeniorInfo> getSeniorInfoList() {
		return seniorInfoList;
	}

	public void setSeniorInfoList(List<SeniorInfo> seniorInfoList) {
		this.seniorInfoList = seniorInfoList;
	}

	public List<JuniorInfo> getJuniorInfoList() {
		return juniorInfoList;
	}

	public void setJuniorInfoList(List<JuniorInfo> juniorInfoList) {
		this.juniorInfoList = juniorInfoList;
	}

	public List<UniversityInfo> getUniversityInfoList() {
		return universityInfoList;
	}

	public void setUniversityInfoList(List<UniversityInfo> universityInfoList) {
		this.universityInfoList = universityInfoList;
	}

	public List<BasicInfo> getBasicInfoList() {
		return basicInfoList;
	}

	public void setBasicInfoList(List<BasicInfo> basicInfoList) {
		this.basicInfoList = basicInfoList;
	}

	public List<PersonalInfo> getPersonalInfoList() {
		return personalInfoList;
	}

	public void setPersonalInfoList(List<PersonalInfo> personalInfoList) {
		this.personalInfoList = personalInfoList;
	}

	public List<Academy> getAcademyList() {
		return academyList;
	}

	public void setAcademyList(List<Academy> academyList) {
		this.academyList = academyList;
	}

	public List<Senior> getSeniorList() {
		return seniorList;
	}

	public void setSeniorList(List<Senior> seniorList) {
		this.seniorList = seniorList;
	}

	public List<TechnicalSchool> getTechnicalSchoolList() {
		return technicalSchoolList;
	}

	public void setTechnicalSchoolList(List<TechnicalSchool> technicalSchoolList) {
		this.technicalSchoolList = technicalSchoolList;
	}

	public List<Junior> getJuniorList() {
		return juniorList;
	}

	public void setJuniorList(List<Junior> juniorList) {
		this.juniorList = juniorList;
	}

	public List<WorkInfo> getWorkInfoList() {
		return workInfoList;
	}

	public void setWorkInfoList(List<WorkInfo> workInfoList) {
		this.workInfoList = workInfoList;
	}

	public List<Industry> getIndustryList() {
		return industryList;
	}

	public void setIndustryList(List<Industry> industryList) {
		this.industryList = industryList;
	}

	public List<IndustryBranch> getIndustryBranchList() {
		return industryBranchList;
	}

	public void setIndustryBranchList(List<IndustryBranch> industryBranchList) {
		this.industryBranchList = industryBranchList;
	}

	public List<Position> getPositionList() {
		return positionList;
	}

	public void setPositionList(List<Position> positionList) {
		this.positionList = positionList;
	}

	public List<PositionBranch> getPositionBranchList() {
		return positionBranchList;
	}

	public void setPositionBranchList(List<PositionBranch> positionBranchList) {
		this.positionBranchList = positionBranchList;
	}

	public List<ContactInfo> getContactInfoList() {
		return contactInfoList;
	}

	public void setContactInfoList(List<ContactInfo> contactInfoList) {
		this.contactInfoList = contactInfoList;
	}

	public ContactInfo getContactInfo() {
		return contactInfo;
	}

	public void setContactInfo(ContactInfo contactInfo) {
		this.contactInfo = contactInfo;
	}

	public List<City> getHomeCityList() {
		return homeCityList;
	}

	public void setHomeCityList(List<City> homeCityList) {
		this.homeCityList = homeCityList;
	}

	public List<City> getLocationCityList() {
		return locationCityList;
	}

	public void setLocationCityList(List<City> locationCityList) {
		this.locationCityList = locationCityList;
	}

	
	public String getBirthDay() {
		return birthDay;
	}

	public void setBirthDay(String birthDay) {
		this.birthDay = birthDay;
	}

	public String getBirthYear() {
		return birthYear;
	}

	public void setBirthYear(String birthYear) {
		this.birthYear = birthYear;
	}

	public String getBirthMonth() {
		return birthMonth;
	}

	public void setBirthMonth(String birthMonth) {
		this.birthMonth = birthMonth;
	}

	public List<Year> getYearList() {
		return yearList;
	}

	public void setYearList(List<Year> yearList) {
		this.yearList = yearList;
	}

	public List<Month> getMonthList() {
		return monthList;
	}

	public void setMonthList(List<Month> monthList) {
		this.monthList = monthList;
	}

	public List<Day> getDayList() {
		return dayList;
	}

	public void setDayList(List<Day> dayList) {
		this.dayList = dayList;
	}


	public List<Identify> getIdentifyList() {
		return identifyList;
	}


	public void setIdentifyList(List<Identify> identifyList) {
		this.identifyList = identifyList;
	}


	public List<Domitory> getDomitoryList() {
		return domitoryList;
	}


	public void setDomitoryList(List<Domitory> domitoryList) {
		this.domitoryList = domitoryList;
	}


	public Map<Integer, List<Domitory>> getDomitoryMap() {
		return domitoryMap;
	}


	public void setDomitoryMap(Map<Integer, List<Domitory>> domitoryMap) {
		this.domitoryMap = domitoryMap;
	}


	public Map<Integer, List<Academy>> getAcademyMap() {
		return academyMap;
	}


	public void setAcademyMap(Map<Integer, List<Academy>> academyMap) {
		this.academyMap = academyMap;
	}


	public University getUniversity() {
		return university;
	}


	public void setUniversity(University university) {
		this.university = university;
	}


	public UniversityInfo getUniversityInfo() {
		return universityInfo;
	}


	public void setUniversityInfo(UniversityInfo universityInfo) {
		this.universityInfo = universityInfo;
	}


	public int getUniversityId() {
		return universityId;
	}


	public void setUniversityId(int universityId) {
		this.universityId = universityId;
	}


	public Map<Integer, List<PositionBranch>> getPositionBranchMap() {
		return positionBranchMap;
	}


	public void setPositionBranchMap(
			Map<Integer, List<PositionBranch>> positionBranchMap) {
		this.positionBranchMap = positionBranchMap;
	}


	public Map<Integer, List<IndustryBranch>> getIndustryBranchMap() {
		return industryBranchMap;
	}


	public void setIndustryBranchMap(
			Map<Integer, List<IndustryBranch>> industryBranchMap) {
		this.industryBranchMap = industryBranchMap;
	}


	public int getPositionId() {
		return positionId;
	}


	public void setPositionId(int positionId) {
		this.positionId = positionId;
	}


	public int getIndustryId() {
		return industryId;
	}


	public void setIndustryId(int industryId) {
		this.industryId = industryId;
	}


	public int getWorkInfoId() {
		return workInfoId;
	}


	public void setWorkInfoId(int workInfoId) {
		this.workInfoId = workInfoId;
	}


	public WorkInfo getWorkInfo() {
		return workInfo;
	}


	public void setWorkInfo(WorkInfo workInfo) {
		this.workInfo = workInfo;
	}
	
	
}
